<?php

namespace App\Api\Sms;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;
use Monolog\Logger;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use App\Repository\Worker\ReporyOrderRepository;

class TransferSms
{
  public $model = null;
  private $root;
  public function __construct()
  {
    $this->root = new Logger('threeWaySmsLog');
    $this->root->pushHandler(
      (new RotatingFileHandler(storage_path('/logs/threeWaySmsLog.log'), 14, Logger::DEBUG, true, 0777))->setFormatter(new LineFormatter(null, null, true, true))
    );
  }
  public function send($data)
  {
    if (empty($data['name'])) {
      return $this->returnMsg(101, '请传递通道名称');
    }
    // $channelArr = DB::table(Prefix . 'wlksmschannel')->where(['name' => $data['name']])->first();
    // if (empty($channelArr)) {
    //   return $this->returnMsg(104, '未查询到通道');
    // }
    // if (empty($channelArr->name)) {
    //   return $this->returnMsg(105, '通道配置错误');
    // }
    // $pname = $channelArr->name;
    $model = new SMSChannel();
    $this->model = $model->loadChannel($data['name']);
    if (empty($this->model)) {
      return $this->returnMsg(105, '未查询到通道');
    }
    return $this->returnMsg(200, '');
  }
  public function __call($name, $arguments)
  {
    $data = !empty($arguments[0]) ? $arguments[0] : [];
    $sms_way = '';

    //判断传递过来的值是否存在 is_yzm 和 shop_id
    if (!array_key_exists('is_yzm', $data)) {
      $data['is_yzm'] = 0;
    }
    if (array_key_exists('is_yzm', $data)) {

      $where = [
        'is_yzm' => $data['is_yzm'],
      ];
      if (!empty($data['shop_id'])) {
        $where['shop_id'] = $data['shop_id'];
      }
      //先去数据库里查询
      $sms_way = DB::table('sms_way')->where($where)->first();
      $this->root->info('model' . json_encode($data) . '返回结果1:' . json_encode($sms_way) . '名称:' . !empty($sms_way->name) ? $sms_way->name : '111');
    }

    if (!empty($sms_way)) {
      $model = $this->send(['name' => $sms_way->name]);

      // 查到之后去调用数据库中的
      $params = strToKeyValArr($sms_way->parame);

      $params['url'] = $sms_way->url;

      $d = array_merge((array) $data, $params);

      // $model = $this->send($data);
      $this->root->info('短信发送参数1' . json_encode($data) . '返回结果1:' . json_encode($model));
      $res = $this->model->$name($d);
      $this->root->info('短信发送参数' . json_encode($d) . '返回结果:' . json_encode($res));
      return $res;
    } else {

      $model = $this->send($data);

      if (!empty($model['code']) && $model['code'] == 200) {
        //去数据库中读取发送参数
        if (!array_key_exists('sms_type', $data)) {
          $dataModel = [
            'user_url' => 'http://121.89.236.101:8080/eums/sms/utf8/send.do',
            'user_name' => '401325',
            'user_pass' => '7b0pb8i4'
          ];
          if (!empty($data['is_yam']) && $data['is_yam']) {
            $dataModel = [
              'user_url' => 'http://121.89.236.101:8080/eums/sms/utf8/send.do',
              'user_name' => '401460',
              'user_pass' => 'cb3ufunt'
            ];
          }
          $d = array_merge((array) $data, $dataModel);

          $res = $this->model->$name($d);
          $this->root->info('短信发送参数' . json_encode($d) . '返回结果:' . json_encode($res));
          return $res;
        }
      }
    }
    return $model;
  }
  public function returnMsg($code = 200, $msg = '', $data = [])
  {
    return [
      'code' => $code,
      'msg' => $msg,
      'data' => $data
    ];
  }
}
